﻿//using CleanArchitecture.Infrastructure.Services.Media.RoxyFileman;
//using CleanArchitecture.Web.Filters;
//using Microsoft.AspNetCore.Http;
//using Microsoft.AspNetCore.Mvc;
//using Microsoft.Extensions.Primitives;
//using System;
//using System.Threading.Tasks;

//namespace CleanArchitecture.Web.Controllers.Backend
//{
//    //Controller for Roxy fileman (http://www.roxyfileman.com/) for TinyMCE editor
//    //the original file was \RoxyFileman-1.4.5-net\fileman\asp_net\main.ashx

//    //do not validate request token (XSRF)
//    [PublicAntiForgery(true)]
//    public class RoxyFilemanController : BaseAdminController
//    {
//        #region Fields
//        private readonly IRoxyFilemanService _roxyFilemanService;

//        #endregion

//        #region Ctor

//        public RoxyFilemanController(IRoxyFilemanService roxyFilemanService)
//        {
//            _roxyFilemanService = roxyFilemanService;
//        }

//        #endregion

//        #region Methods

//        /// <summary>
//        /// Create configuration file for RoxyFileman
//        /// </summary>
//        public virtual void CreateConfiguration()
//        {
//            _roxyFilemanService.CreateConfiguration();
//        }

//        /// <summary>
//        /// Process request
//        /// </summary>
//        public virtual void ProcessRequest()
//        {
//            //async requests are disabled in the js code, so use .Wait() method here
//            ProcessRequestAsync().Wait();
//        }

//        /// <summary>
//        /// DavidLee 2019/12/08 重写缩略图生成显示方法，nopCommece方法在response时无法正常显示缩略图
//        /// </summary>
//        /// <param name="f"></param>
//        /// <param name="width"></param>
//        /// <param name="height"></param>
//        /// <returns></returns>
//        public virtual IActionResult GenerateThumb(string f, int width, int height)
//        {
//            var cropImageBuffer = _roxyFilemanService.CreateImageThumbnail(f, width: width, height: height);
//            return new FileStreamResult(cropImageBuffer, "image/png");
//        }
//        #endregion

//        #region Utilities

//        /// <summary>
//        /// Process the incoming request
//        /// </summary>
//        /// <returns>A task that represents the completion of the operation</returns>
//        protected virtual async Task ProcessRequestAsync()
//        {
//            var action = "DIRLIST";
//            try
//            {
//                //if (!_permissionService.Authorize(StandardPermissionProvider.HtmlEditorManagePictures))
//                //    throw new Exception("You don't have required permission");
//                if (!StringValues.IsNullOrEmpty(HttpContext.Request.Query["a"]))
//                    action = HttpContext.Request.Query["a"];

//                switch (action.ToUpper())
//                {
//                    case "DIRLIST":
//                        await _roxyFilemanService.GetDirectoriesAsync(HttpContext.Request.Query["type"]);
//                        break;
//                    case "FILESLIST":
//                        await _roxyFilemanService.GetFilesAsync(HttpContext.Request.Query["d"], HttpContext.Request.Query["type"]);
//                        break;
//                    case "COPYDIR":
//                        await _roxyFilemanService.CopyDirectoryAsync(HttpContext.Request.Query["d"], HttpContext.Request.Query["n"]);
//                        break;
//                    case "COPYFILE":
//                        await _roxyFilemanService.CopyFileAsync(HttpContext.Request.Query["f"], HttpContext.Request.Query["n"]);
//                        break;
//                    case "CREATEDIR":
//                        await _roxyFilemanService.CreateDirectoryAsync(HttpContext.Request.Query["d"], HttpContext.Request.Query["n"]);
//                        break;
//                    case "DELETEDIR":
//                        await _roxyFilemanService.DeleteDirectoryAsync(HttpContext.Request.Query["d"]);
//                        break;
//                    case "DELETEFILE":
//                        await _roxyFilemanService.DeleteFileAsync(HttpContext.Request.Query["f"]);
//                        break;
//                    case "DOWNLOAD":
//                        await _roxyFilemanService.DownloadFileAsync(HttpContext.Request.Query["f"]);
//                        break;
//                    case "DOWNLOADDIR":
//                        await _roxyFilemanService.DownloadDirectoryAsync(HttpContext.Request.Query["d"]);
//                        break;
//                    case "MOVEDIR":
//                        await _roxyFilemanService.MoveDirectoryAsync(HttpContext.Request.Query["d"], HttpContext.Request.Query["n"]);
//                        break;
//                    case "MOVEFILE":
//                        await _roxyFilemanService.MoveFileAsync(HttpContext.Request.Query["f"], HttpContext.Request.Query["n"]);
//                        break;
//                    case "RENAMEDIR":
//                        await _roxyFilemanService.RenameDirectoryAsync(HttpContext.Request.Query["d"], HttpContext.Request.Query["n"]);
//                        break;
//                    case "RENAMEFILE":
//                        await _roxyFilemanService.RenameFileAsync(HttpContext.Request.Query["f"], HttpContext.Request.Query["n"]);
//                        break;
//                    //case "GENERATETHUMB":
//                    //    _roxyFilemanService.CreateImageThumbnail(HttpContext.Request.Query["f"]);
//                    //    break;
//                    case "UPLOAD":
//                        await _roxyFilemanService.UploadFilesAsync(HttpContext.Request.Form["d"]);
//                        break;
//                    default:
//                        await HttpContext.Response.WriteAsync(_roxyFilemanService.GetErrorResponse("This action is not implemented."));
//                        break;
//                }
//            }
//            catch (Exception ex)
//            {
//                if (action == "UPLOAD" && !_roxyFilemanService.IsAjaxRequest())
//                    await HttpContext.Response.WriteAsync($"<script>parent.fileUploaded({_roxyFilemanService.GetErrorResponse(_roxyFilemanService.GetLanguageResource("E_UploadNoFiles"))});</script>");
//                else
//                    await HttpContext.Response.WriteAsync(_roxyFilemanService.GetErrorResponse(ex.Message));
//            }
//        }

//        #endregion
//    }
//}